<?php
/**
 * 组件管理Model类
 * @file		ComponentsModel.class.php
 * @author     	kelecode@gmail.com
 * @date       	2013-06-14
 * modify       
 */
class ComponentsModel extends Model{

	private $table = array();
	public $pageSize;

	public function __construct(){
		parent::__construct();

		$this->table = array(
			'components' => C('db_table_prefix').'components',
			'extensions' => C('db_table_prefix').'extensions',
		);
		$this->pageSize = C('page.page_size');
	}
	
	/**
	 * 获取单条或全部组件列表
	 * @param int $id
	 * @return array $lists
	 */
	public function extensionsList($id=null){
		$pageSize = $this->pageSize;
		$currentPage = (int)$_GET[C('page.var_page')];
		$currentPage = $currentPage <= 0 ? 1 : $currentPage;
		$start = ($currentPage - 1) * $pageSize;
        $start = $start < 0 ? 0 : $start;
		
		$lists = array();
		$sql = "SELECT count(*) AS count FROM `{$this->table['extensions']}` WHERE `status`=1";
		if($id){
			$sql .= " AND `extension_id`='{$id}'";
		}
		$count = $this->query($sql);
        $count = $count[0]['count'];		
		$sql = str_replace('count(*) AS count','*',$sql);
        $sql .= " ORDER BY `extension_id` ASC LIMIT {$start},{$pageSize}";
		$lists = $this->query($sql);

		return array('count' => $count,'lists' => $lists);
	}
	
	public function install($data){
		if( empty($data) ){
			return false;
		}
		
		$sql = 'INSERT INTO `'.$this->table['extensions'].'` (`'.implode('`,`',array_keys($data)).'`) VALUES ("'.implode('","',array_values($data)).'")';
		$result = $this->execute($sql);
		
		if($result){
			//TODO::			
		}
		
		return $result;
	}
	
	public function uninstall(){
	
	}
	
	
}